﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using Telerik.WebControls;
using UFIDA.UBF.Query.CaseModel;
using UFIDA.UBF.Query.CommonService;
using UFIDA.UBF.Query.CommonService.QueryStrategy;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Report.Filter.FilterModel;
using UFSoft.UBF.UI.ActionProcess;
using UFSoft.UBF.UI.ControlModel;
using UFSoft.UBF.UI.Controls;
using UFSoft.UBF.UI.Engine;
using UFSoft.UBF.UI.FormProcess;
using UFSoft.UBF.UI.IView;
using UFSoft.UBF.UI.MD.Runtime;
using UFSoft.UBF.UI.WebControls;
using UFSoft.UBF.UI.WebControls.ClientCallBack;
using UFSoft.UBF.Util.DataAccess;
using UFSoft.UBF.Util.Globalization;
using UFSoft.UBF.Util.Log;



/***********************************************************************************************
 * Form ID: 
 * UIFactory Auto Generator 
 ***********************************************************************************************/
namespace UFIDA.U9.Cust.HWProject.CreateAPBillHead.CreateAPBilListUIModel
{
    public partial class CreateAPBilListUIFormWebPart
    {
        #region Custome eventBind
	
		 
			
				

		//DataGrid_GridRowDbClicked...
		private void DataGrid_GridRowClicked_Extend(object sender, GridCallBackEventArgs e)
		{
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            //点击时只展示这行的子行数据
            if (this.Model.APbilHead.FocusedRecord!=null)
            {
                long ID = this.Model.APbilHead.FocusedRecord.ID;
                this.Model.APbilHead_APbilLine.CurrentFilter.OPath = string.Format("APbilHead='{0}'",ID); ;
            }
            else
            {
                throw new Exception("请选择有数据的行");
            }
		     DataGrid_GridRowClicked_DefaultImpl(sender,e);
            //DataGrid_GridRowDbClicked_DefaultImpl(sender,e);
		}	
		 
				//OnSelect_Click...
		private void OnSelect_Click_Extend(object sender, EventArgs  e)
		{
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            UFIDA.U9.UI.Commands.ICommand cmd = UFIDA.U9.UI.Commands.CommandFactory.GetCommand("QryClick");
            if (cmd != null)
            {
                cmd.Initialize(this.Action);
                cmd.Do(sender, e);
                cmd.Destroy();
            }

            OnSelect_Click_DefaultImpl(sender,e);
		}	
		 
				//OnToSRM_Click...
		private void OnToSRM_Click_Extend(object sender, EventArgs  e)
		{
            this.Model.ClearErrorMessage();
            using (new BPForEngine())
            {
                try
                {
                    List<string> SrcSystemTypes = new List<string>();
                    if (this.Model.APbilHead.Cache.GetSelectRecord() == null)
                    {
                        throw new Exception("请先选择需要推送的数据");
                    }
                    else
                    {
                        IList<IUIRecord> recs = this.Model.APbilHead.Cache.GetSelectRecord();
                        foreach (var item in recs)
                        {
                            if (item["IsSuccess"].ToString().ToUpper().Equals("TRUE"))
                            {
                                throw new Exception(item["SrcSystemType"].ToString() + "已正确生成单据，请勿重复推送");
                            }
                            else
                            {
                                SrcSystemTypes.Add(item["SrcSystemType"].ToString());
                            }
                        }
                    }
                    //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
                    UFIDA.U9.Cust.HWProject.CreateAPBillHead.CreateAPBilBP.CreateAPBil createAPBilBP = new UFIDA.U9.Cust.HWProject.CreateAPBillHead.CreateAPBilBP.CreateAPBil();
                    createAPBilBP.SrcSystemType = SrcSystemTypes;
                    if (createAPBilBP.Do()=="200")
                    {
                        this.Action.CurrentPart.ShowWindowStatus("应付单推送完成！", true, true);
                    }
                    this.Action.NavigateAction.Refresh(null, true);
                }
                catch (Exception error)
                {

                    throw new Exception(error.Message);
                }
            }

            OnToSRM_Click_DefaultImpl(sender,e);
		}

		
            

		#region 自定义数据初始化加载和数据收集
		private void OnLoadData_Extend(object sender)
		{
          //  this.Action.InitCurrentViewCaseModel();
            InitCaseModelShortageOrder();
            this.Model.APbilHead.Clear();
            OnLoadData_DefaultImpl(sender);
        }
		private void OnDataCollect_Extend(object sender)
		{	
			OnDataCollect_DefaultImpl(sender);
		}
		#endregion  

        #region 自己扩展 Extended Event handler 
		public void AfterOnLoad()
		{
            IUIModelProperty mp = this.Model.Properties[UFIDA.UBF.Query.CommonService.QuerySymbol.NeedRefresh];
            if ((mp != null && (bool)mp.Value == true))
            {
                //this.Model.APbilHead.Clear();
                this.Model.APbilHead.CurrentFilter.OrderBy = QryService.GetOrderByOpath((BaseWebForm)(this));
                this.Model.APbilHead.CurrentFilter.OPath = LoadByQryModelShortageOrder();
                this.Action.CommonAction.Load(this.Model.APbilHead);
            }
            this.Action.NavigateAction.Refresh(null, true);
        }

        public void AfterCreateChildControls()
        {
            //开启个性化：不加这段代码，页面无法使用个性化
            UFIDA.U9.UI.PDHelper.PersonalizationHelper.SetPersonalizationEnable((BaseWebForm)this, true);


        }
        
        public void AfterEventBind()
        {
        }
        
		public void BeforeUIModelBinding()
        { 
            if (!Page.IsPostBack)
            {
                //设置过滤条件，可以根据实际情况设定过虑 条件

                //this.Model.APbilHead.CurrentFilter.OPath = " 1 =1  ";
                this.Action.CommonAction.Load();
            }
        }

		public void AfterUIModelBinding()
		{
            using (new BPForEngine())
            {
                this.Model.APbilHead.Clear();
                this.Action.NavigateAction.Refresh(null, true);

                //this.DataCollect();
                //this.DataBind();
            }

        }


        private void InitCaseModelShortageOrder()
        {
            QryService.ClearSession((UFSoft.UBF.UI.FormProcess.BaseWebForm)(this));
            string EntityFullName = "UFIDA.U9.Cust.HWProject.CreateAPBillHead.APBil.APbilHead";
            string QryModelID = "19d9004b-5919-43d4-abb6-920a79708043";
            IUIView UIView = this.Model.APbilHead;

            IUFDataGrid grid = this.DataGrid;
            this.Model.APbilHead.CurrentFilter.OrderBy = QryService.GetOrderByOpath((BaseWebForm)(this));
            BEQueryStrategyImpl beQryStrategyImpl = new BEQueryStrategyImpl(this.CurrentState, EntityFullName, QryModelID, UIView, grid, 1);

            //beQryStrategyImpl.ColumnTabIsVisible = true;
            beQryStrategyImpl.SortTabIsVisible = true;

            CaseModel cm = beQryStrategyImpl.GetQryModel();
            cm.PageSizeEnable = false;
            cm.SumRowType = SumRowType.None;
            cm.ColumnTabIsVisible = false;
            cm.SortDefineGridVisible = true;
            cm.SortTabIsVisible = true;
            //QryService.SetCaseModelToSession((UFSoft.UBF.UI.FormProcess.BaseWebForm)(this), cm);
            QryService.SetCaseModelToSession((IPart)(grid.CurrentPart), cm);
        }

        private string LoadByQryModelShortageOrder()
        {
            string where = "";
            string QryModelID = "19d9004b-5919-43d4-abb6-920a79708043";
            CaseModel cm = QryService.GetCaseModelFromSession(this, QryModelID);
            string strOpath = string.Empty;
            strOpath = FilterBuilder.GetInstance(cm.Case.FilterValues, cm.Case.FilterDefines).GetWhereString();
            if (string.IsNullOrEmpty(strOpath))
            {
                strOpath = where;
            }
            else
            {
                where = strOpath;
            }

            return where;
        }

        private string GetOPath(string str)
        {
            string opath = "";

            if (!string.IsNullOrEmpty(this.Model.APbilHead.CurrentFilter.OPath))
            {
                opath = this.Model.APbilHead.CurrentFilter.OPath;
            }
            return opath;
        }
        #endregion

        #endregion

    }
}